/**
 * 
 */
package idv.takeshi.software.productline.bookstore.presentation.web.tag;

import idv.takeshi.software.productline.bookstore.domain.model.book.Book;
import idv.takeshi.software.productline.bookstore.domain.model.category.Category;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * @author takeshi
 *
 */
public class PathCategoriesTagHandler extends AbstractSimpleTagSupport {
	
	private static Log log = LogFactory.getLog(PathCategoriesTagHandler.class);
	
	private String id;
	
	private String uri;
	
	private Set<Category> selectedParentCategories;
	
	private Book selectedBook;
	

	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.SimpleTagSupport#doTag()
	 */
	@Override
	public void doTag() throws JspException, IOException {
		final String msg = "generate path categories content failed";
		try {
			super.doTag();
			JspWriter writer = this.getJspContext().getOut();
			if(StringUtils.isBlank(this.id))
				writer.println("<div>");
			else
				writer.println("<div id=\"" + this.id + "\">");
			
			if(CollectionUtils.isNotEmpty(selectedParentCategories)){
				Category category = null;
				int a = 0;
				for(Iterator<Category> it = this.selectedParentCategories.iterator(); it.hasNext();){
					category = it.next();
					if(a > 0){
						if(a > 1)
							writer.print(">");
						writer.print("<a href=\"" + this.uri + category.getId() + "\">");
						writer.println(category.getName().getText(getLocaleString()) + "</a>");
					}
					a++;
				}
				if(null != this.selectedBook){
					writer.print(">");
					writer.println(this.selectedBook.getName());
				}
			}
			
			writer.println("</div>");
		} catch(JspException e){
			log.error(msg, e);
			throw e;
		} catch(IOException e){
			log.error(msg, e);
			throw e;
		}
	}


	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}


	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}


	/**
	 * @return the uri
	 */
	public String getUri() {
		return uri;
	}


	/**
	 * @param uri the uri to set
	 */
	public void setUri(String uri) {
		this.uri = uri;
	}


	/**
	 * @return the selectedParentCategories
	 */
	public Set<Category> getSelectedParentCategories() {
		return selectedParentCategories;
	}


	/**
	 * @param selectedParentCategories the selectedParentCategories to set
	 */
	public void setSelectedParentCategories(Set<Category> selectedParentCategories) {
		this.selectedParentCategories = selectedParentCategories;
	}


	/**
	 * @return the selectedBook
	 */
	public Book getSelectedBook() {
		return selectedBook;
	}


	/**
	 * @param selectedBook the selectedBook to set
	 */
	public void setSelectedBook(Book selectedBook) {
		this.selectedBook = selectedBook;
	}
	
	

}
